package com.datawin.taotaocheche.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.util.List;

public class IsInstallUtils {

    /**
     * 检测微信是否安装
     *
     * @param context
     * @param api
     * @return
     */
    public static boolean isWXAppInstalledAndSupported(Context context,
                                                       IWXAPI api) {
        boolean sIsWXAppInstalledAndSupported = api.isWXAppInstalled()
                && api.isWXAppSupportAPI();
        if (!sIsWXAppInstalledAndSupported) {
            ToastUtils.shortToast("未发现微信客户端，请检查是否安装！");
        }

        return sIsWXAppInstalledAndSupported;
    }

    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        ToastUtils.shortToast("未检测到微信客户端");
        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        ToastUtils.shortToast("未检测到QQ客户端");
        return false;
    }

    /**
     * 判断微博是否安装
     */
    public static boolean isWBClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.sina.weibo")) {
                    return true;
                }
            }
        }
        ToastUtils.shortToast("未检测到新浪微博客户端");
        return false;
    }

    /**
     * 判断是否安装目标应用
     *
     * @param
     * @return 是否已安装目标应用
     */

    public static boolean isInstallByread(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                System.out.println(pinfo.get(i));
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.baidu.BaiduMap")) {
                    return true;
                }
            }
        }
        return false;
    }

}
